﻿using System;
using Newtonsoft.Json;

namespace CometDocs
{
    /// <summary>
    /// Converter for the ConversionType class.
    /// Indeed the CometDocs web API represents conversion types as strings: e.g. "PDF2XLS" is ConversionType{ From = "PDF", To = "XLS" }.
    /// </summary>
    internal class ConversionTypeJsonConverter : JsonConverter
    {
        public override bool CanConvert(Type objectType)
        {
            return objectType == typeof(ConversionType);
        }

        public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
        {
            string s = reader.Value as string;

            return ConversionType.FromString(s);
        }

        public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
        {
            writer.WriteComment(value.ToString());
        }
    }
}
